package com.lopo.mybatis_auto_insert;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.lopo.bean.TestBean;
import com.lopo.mapper.DateMapper;
import com.lopo.repeat_submit.RepeatSubmit;
import com.lopo.util.JackSonUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

@RestController
public class InstrtController {
    @Autowired
    private DateMapper dateMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/public/ins")
    @RepeatSubmit
    public String ddd() {
        final TestBean ddd = TestBean.builder().name("ddd").build();
        dateMapper.update("ssd");
        return "ss";
    }

    @GetMapping("/public/a/{id}")
    public String a(@PathVariable("id") Long id, String dd, TestBean testBean) {
//        final TestBean ddd = TestBean.builder().name("ddd").build();
//        dateMapper.update("ssd");
        System.out.println("id = " + id);
        return "ss";
    }

    @GetMapping("/public/a")
    @SneakyThrows
    public void aaa() {
        final String methodName = (String) redisTemplate.opsForHash().get("sn", "methodName");
        final String s = (String) redisTemplate.opsForHash().get("sn", "args");
        Object[] args = JackSonUtils.fromJson(s, Object[].class);
        final String controllerName = (String) redisTemplate.opsForHash().get("sn", "controllerName");
        final String parameterType = (String) redisTemplate.opsForHash().get("sn", "parameterTypes");
        Class[] parameterTypes = JackSonUtils.fromJson(parameterType, Class[].class);

        final Object bean = SpringUtil.getBean(Class.forName(controllerName));
        final Method method = bean.getClass().getMethod(methodName, parameterTypes);
        method.setAccessible(true);
//        method.invoke(bean, args);

        ReflectUtil.invoke(bean,method,args);
    }

}
